<?php

/**
 * Created by PhpStorm.
 * User: cuong_000
 * Date: 1/11/2015
 * Time: 8:18 PM
 */
class Custom_order extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model(array('Custom_order_model'));
        $query =
            "CREATE TABLE IF NOT EXISTS " . $this->db->dbprefix('custom_order') . " (
            custom_order_id int(11) NOT NULL auto_increment,
            custom_order_number varchar(60) CHARACTER SET utf8 NOT NULL,
            name varchar(255) CHARACTER SET utf8 NOT NULL,
            email varchar(255) CHARACTER SET utf8 NOT NULL,
            phone varchar(255) CHARACTER SET utf8 NOT NULL,
            address varchar(500) CHARACTER SET utf8 NOT NULL,
            stats varchar(100) CHARACTER SET utf8 NOT NULL,
            total float(10,2),
            datetime datetime NOT NULL,
            content text CHARACTER SET utf8 NOT NULL,
            message text CHARACTER SET utf8 NOT NULL,
            admin_note text CHARACTER SET utf8 NOT NULL,
            customer_id int(11) NULL,
            PRIMARY KEY (custom_order_id)
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10;";
        $this->db->query($query);
        $query =
            "CREATE TABLE IF NOT EXISTS " . $this->db->dbprefix('custom_order_status') . " (
            custom_order_status_id int(11) NOT NULL auto_increment,
            custom_order_number varchar(255) CHARACTER SET utf8 NOT NULL,
            PRIMARY KEY (custom_order_status_id)
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10;";
        $this->db->query($query);
    }

    function index($order_by = "custom_order_id", $sort_order = "DESC", $code = 0, $page = 0, $rows = 10)
    {
        $data = array();
        $data['order_by'] = $order_by;
        $data['sort_order'] = $sort_order;
        $data['entries'] = $this->Custom_order_model->view(array('order_by' => $order_by, 'sort_order' => $sort_order, 'rows' => $rows, 'page' => $page));
        $data['total'] = $this->Custom_order_model->view(array('order_by' => $order_by, 'sort_order' => $sort_order), true);

        $this->load->library('pagination');
        $config['base_url'] = site_url($this->config->item('admin_folder') . '/custom_order/index/' . $order_by . '/' . $sort_order . '/' . $code . '/');
        $config['total_rows'] = $data['total'];
        $config['per_page'] = $rows;
        $config['uri_segment'] = 7;
        $config['first_link'] = 'First';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Last';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['full_tag_open'] = '<div class="pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['prev_link'] = '&laquo;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';

        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';

        $this->pagination->initialize($config);
        $this->view($this->config->item('admin_folder') . '/custom_order', $data);
    }

    function viewdetails($id)
    {
        // get the list of canned messages (order)
        $this->load->model('Messages_model');
        $msg_templates = $this->Messages_model->get_list('');
        $data['order'] = $this->Custom_order_model->viewdetails($id);
        if ($_POST) {
            $data['order']->admin_note = $this->input->post('admin_note');
            $data['order']->total = $this->input->post('total');
            $data['order']->status = $this->input->post('status');
            $this->Custom_order_model->update($data['order']);
            $this->session->set_flashdata('message', 'Cập nhận order: ' . $data['order']->custom_order_number . 'Thành công.');
        }
        // replace template variables
        foreach ($msg_templates as $msg) {
            // fix html
            $msg['content'] = str_replace("\n", '', html_entity_decode($msg['content']));

            // {order_number}
            $msg['subject'] = str_replace('{order_number}', $data['order']->custom_order_number, $msg['subject']);
            $msg['content'] = str_replace('{order_number}', $data['order']->custom_order_number, $msg['content']);

            // {url}
            $msg['subject'] = str_replace('{url}', $this->config->item('base_url'), $msg['subject']);
            $msg['content'] = str_replace('{url}', $this->config->item('base_url'), $msg['content']);

            // {site_name}
            $msg['subject'] = str_replace('{site_name}', $this->config->item('company_name'), $msg['subject']);
            $msg['content'] = str_replace('{site_name}', $this->config->item('company_name'), $msg['content']);

            $data['msg_templates'][] = $msg;
        }
        $this->view($this->config->item('admin_folder') . '/custom_order_details', $data);
    }

    function update_admin_note($custom_order_id = '')
    {
        $order = $this->Custom_order_model->viewdetails($custom_order_id);

        redirect($this->config->item('admin_folder') . '/custom_order/viewdetails/' . $custom_order_id);
    }

    function send_response($custom_order_id = '')
    {
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => '2thang7',
            'smtp_pass' => 'Cuong123',
            'mailtype' => 'html',
            'charset' => 'utf-8'
        );

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");

        $this->email->from('2thang7@gmail.com', 'Hàng Order');
        $this->email->to($this->input->post('recipient'));

        $this->email->subject($this->input->post('subject'));
        $this->email->message(html_entity_decode($this->input->post('content')));

        $this->email->send();

        $this->session->set_flashdata('message', 'Phản hồi đã được gửi đến: ' . $this->input->post('recipient'));
        redirect($this->config->item('admin_folder') . '/custom_order/viewdetails/' . $custom_order_id);
    }

    function bulk_delete()
    {
        $orders = $this->input->post('order');

        if ($orders) {
            foreach ($orders as $order) {
                $this->Custom_order_model->delete($order);
            }
            $this->session->set_flashdata('message', 'Những gợi ý được chọn đã bị xóa!');
        } else {
            $this->session->set_flashdata('error', 'Bạn chưa chọn gợi ý nào!');
        }
        //redirect as to change the url
        redirect($this->config->item('admin_folder') . '/custom_order');
    }
}